package me.huha.zhime.service.impl;

import me.huha.ibatis.sqlbuilder.SpecificationBuilder;
import me.huha.tools.BeanMapper;
import me.huha.zhime.commons.OrderInfoDTO;
import me.huha.zhime.entity.OrderInfoEntity;
import me.huha.zhime.entity.OrderInfoEntity.PaymentStatusEnum;
import me.huha.zhime.entity.OrderInfoEntity_;
import me.huha.zhime.service.OrderService;

import org.springframework.stereotype.Service;

/**
 * @Title: OrderServiceImpl
 * @Author: Yepx
 * @mail: yepx2013@gmail.com
 * @CreateTime: 2017年10月26日 下午4:25:34
 * @Todo: TODO
 */
@Service("orderService")
public class OrderServiceImpl implements OrderService {
	
	@Override
	public Boolean createOrUpdateOrderInfo(OrderInfoDTO dto) {
		
		String orderNum = dto.getOrderNum();
		
		OrderInfoEntity entity = OrderInfoEntity.create(OrderInfoEntity.class)
												.findUnique(SpecificationBuilder.builder()
												.where(OrderInfoEntity_.orderNum.eq(orderNum)).build(), null);
		
		if(null == entity){
			entity = new OrderInfoEntity();
			entity.setPaymentStatus(PaymentStatusEnum.unpaid.getStatus());
		}else{
			dto.setOrderCreate(null);
			dto.setOrderClose(null);
		}
		
		BeanMapper.copy(dto, entity);
		entity.createOrUpdate();
		
		return true;
	}
	
	@Override
	public Boolean updateOrderPaymentStatus(String orderNum, PaymentStatusEnum enum_){
		
		OrderInfoEntity entity = OrderInfoEntity.create(OrderInfoEntity.class)
												.findUnique(SpecificationBuilder.builder()
												.where(OrderInfoEntity_.orderNum.eq(orderNum)).build(), null);
		
		if(null != entity){
			entity.setPaymentStatus(enum_.getStatus());
			entity.createOrUpdate();
			return true;
		}else{

			return false;
		}
		
	}
}
